
/* define bounds in IU */


/*      Outlet Pressure Bounds

                 IU      KPa   dynes/cm2     PSI
         -----------------------------------------
MIN    256*6   1536   49.152   4915200    7.128895 
MAX    256*10  4245                      19.75      

        Inlet Pressure Bounds
                 IU      KPa   dynes/cm2     PSI
         -----------------------------------------
MIN    256*0       0     0             0    0.0    
MAX    256*255 65280  2088.960  20889600  302.978046



        Gas Compression Factor  j
       --------------------------------------------
MIN    1    ( Po = 0 )
MAX    4/3  ( Pi = Po )

         j Delta P
       --------------------------------------------
MIN    0    Pi = Po   
MAX    
*/     



#define PASCALS_PER_IU  32
#define DCM2_PER_IU     320

#define MIN_PO_IU       1536   /* 16.3"Hg,  8.02 PSI */
#if 0
#define MAX_PO_IU       3393   /* ?????Hg, 25.74 PSI */
#else
#define MAX_PO_IU       4245   /* ?????Hg, 19.74 PSI */
#endif

#define MIN_PO_DCM2      ( MIN_PO_IU * DCM2_PER_IU )   /* 16.3"Hg,  8.02 PSI */
#define MAX_PO_DCM2      ( MAX_PO_IU * DCM2_PER_IU )   /* 32.0"Hg, 15.74 PSI */

#define IU_PER_PO_ENTRY   (256/4)
#define NUM_PO_ENTRIES     ( 1 + ( MAX_PO_IU - MIN_PO_IU ) / IU_PER_PO_ENTRY )

#define MIN_PI_IU        0
#define MAX_PI_IU        62144       /*  288.42 PSI absolute, 272.68 PSI @ MaxPo */

#define MIN_PI_DCM2       ( MIN_PI_IU * DCM2_PER_IU )
#define MAX_PI_DCM2       ( MAX_PI_IU * DCM2_PER_IU )   /*  288.42 PSI absolute, 272.68 PSI @ MaxPo */

#define IU_PER_PI_ENTRY   256
#define NUM_PI_ENTRIES    257 /* ((MAX_PI_DCM2-MIN_PI_DCM2)/IU_PER_PI_ENTRY)*/


#include <stdio.h>

long  get_pi_tab[NUM_PI_ENTRIES][NUM_PO_ENTRIES];

long  jdp_tab[ NUM_PI_ENTRIES ][ NUM_PO_ENTRIES ];

double j_delta_P ( pi, po )
double pi;
double po;
{
        /* will work for either real or internal units */

     return ( ( (pi*pi + 2*pi*po + po*po)/(pi*pi + pi*po + po*po) )*(pi-po) );

}


long   search ( jdp, po )
double jdp;
double po;
{


   double j_delta_p;
   double pi_guess;
   double delta_pi;
   int i;

   if (po  == 0 ) return ( (long)jdp );
   if (jdp == 0 ) return ( (long)po );

   /*printf ("\npi_guess         po    jdeltaP       jdp   delta_pi\n" );*/


   pi_guess = 4*jdp/3+po ;  
   delta_pi = (pi_guess-1)/2; 

   while ( delta_pi >= .1 )
   {
	j_delta_p = j_delta_P ( pi_guess, po );

	if ( abs(jdp - j_delta_p) < .1 ) break;
        
        /*printf ( "%8.2lf %8.2lf %8.2lf %8.2lf %8.2lf\n", pi_guess/DCM2_PER_IU,
           po/DCM2_PER_IU, j_delta_p/DCM2_PER_IU, jdp/DCM2_PER_IU, delta_pi/DCM2_PER_IU ); */
     
	if ( jdp > j_delta_p ) pi_guess += delta_pi;
	else                   pi_guess -= delta_pi;

        delta_pi = delta_pi / 2;
    }

    /*printf ("\n");*/

    return ( (long)pi_guess );


}

long interpolate( point, x1, x2, y1, y2 )
long point;
long x1;
long x2;
long y1;
long y2;

{

   return ( y1+( (double)(point - x1)*(double)(y2-y1)/(double)(x2-x1) )  );

}

int main ( )
{
   long   jdeltap;
   long   pi, po;
   double jdp, po_psi;
   long jdp_iu, po_iu;
   long answer, estimate;

   for ( jdeltap = 0 ; jdeltap < NUM_PI_ENTRIES  ; jdeltap++ )
   {
       for ( po = 0 ; po < NUM_PO_ENTRIES ; po++ )  
       {

          /* find absolute pressure for a particular jdp */

	  get_pi_tab[jdeltap][po] =
           search ( (double)(MIN_PI_IU + (double)jdeltap*IU_PER_PI_ENTRY),
                    (double)(MIN_PO_IU + (double)po*     IU_PER_PO_ENTRY) );

          /* subtract off the outlet pressure to convert from absolute to guage */
          get_pi_tab[jdeltap][po] -= (double)( MIN_PO_IU + (double)po*IU_PER_PO_ENTRY );

          if ( get_pi_tab[jdeltap][po] > 0xffff ) get_pi_tab[jdeltap][po] = 0xffff;

       }
   }



   for ( pi = 0 ; pi < NUM_PI_ENTRIES  ; pi++ )
   {
       for ( po = 0 ; po < NUM_PO_ENTRIES ; po++ )  
       {
          jdp_tab[pi][po] = j_delta_P ( (double)(MIN_PI_IU + (double)pi * IU_PER_PI_ENTRY) + 
                                        (double)(MIN_PO_IU + (double)po * IU_PER_PO_ENTRY) ,
                                        (double)(MIN_PO_IU + (double)po * IU_PER_PO_ENTRY) );

          if ( jdp_tab[pi][po] > 0xffff ) jdp_tab[pi][po] = 0xffff;

       }
   }



     printf ( "\n" );
     printf ( "#pragma SECTION CONST=flow1\n" );
     printf ( "\n" );
     printf ( "#define PASCALS_PER_IU %d\n", PASCALS_PER_IU );
     printf ( "#define DCM2_PER_IU    %d\n", DCM2_PER_IU );
     printf ( "\n" );
     printf ( "#define MIN_PO_DCM2       %d\n", MIN_PO_DCM2 );
     printf ( "#define MAX_PO_DCM2       %d\n", MAX_PO_DCM2 );
     printf ( "#define IU_PER_PO_ENTRY %d\n", IU_PER_PO_ENTRY );
     printf ( "#define NUM_PO_ENTRIES  %d\n", NUM_PO_ENTRIES );
     printf ( "\n" );
     printf ( "#define MIN_PI_DCM2       %d\n", MIN_PI_DCM2 );
     printf ( "#define MAX_PI_DCM2       %d\n", MAX_PI_DCM2 );
     printf ( "#define IU_PER_PI_ENTRY %d\n", IU_PER_PI_ENTRY );
     printf ( "#define NUM_PI_ENTRIES  %d\n", NUM_PI_ENTRIES );
     printf ( "\n" );


   printf ( "const U_INT16 inlet_pressure_table[NUM_PO_ENTRIES][NUM_PI_ENTRIES] = {\n\n\n\n\n" );


   for ( po = 0 ; po < NUM_PO_ENTRIES ; po++  )  
   {
       printf ("/* po = %8lx @ %3d dcm2_per_iu */  \n", 4*(po*IU_PER_PO_ENTRY*DCM2_PER_IU + MIN_PO_DCM2)/DCM2_PER_IU, DCM2_PER_IU/4 );
       printf ("/* po = %8lx @ %3d dcm2_per_iu */  \n", 2*(po*IU_PER_PO_ENTRY*DCM2_PER_IU + MIN_PO_DCM2)/DCM2_PER_IU, DCM2_PER_IU/2 );
       printf ("/* po = %8lx @ %3d dcm2_per_iu */ {\n",   (po*IU_PER_PO_ENTRY*DCM2_PER_IU + MIN_PO_DCM2)/DCM2_PER_IU, DCM2_PER_IU   );

       for ( jdeltap = 0 ; jdeltap < NUM_PI_ENTRIES ; jdeltap++ )
       {
          if ((jdeltap%8)==0)
              printf ( "\n/*jdp=%5lx*/ ", (long)jdeltap*IU_PER_PI_ENTRY+MIN_PI_DCM2/DCM2_PER_IU );

          if ( jdeltap == (NUM_PI_ENTRIES-1) ) printf ( "0x%-4lx   ", get_pi_tab[jdeltap][po] );
          else                                 printf ( "0x%-4lx,  ", get_pi_tab[jdeltap][po] );

       }
          if ( po == (NUM_PO_ENTRIES-1) ) printf ( " } \n");
          else                            printf ( " },\f\n\n\n");
   }

   printf ( "};\n");

     printf ( "\n" );
   printf ( "#pragma SECTION UNDO\n" );
     printf ( "#pragma SECTION CONST=flow2\n" );
     printf ( "\n" );

   printf ( "\n\n\n\n\nconst U_INT16 jdp_table[NUM_PO_ENTRIES][NUM_PI_ENTRIES] = {\n\n\n\n\n" );


   for ( po = 0 ; po < NUM_PO_ENTRIES ; po++  )  
   {
       printf ("/* po = %8lx @ %3d dcm2_per_iu */  \n", 4*(po*IU_PER_PO_ENTRY + MIN_PO_IU), DCM2_PER_IU/4 );
       printf ("/* po = %8lx @ %3d dcm2_per_iu */  \n", 2*(po*IU_PER_PO_ENTRY + MIN_PO_IU), DCM2_PER_IU/2 );
       printf ("/* po = %8lx @ %3d dcm2_per_iu */ {\n",   (po*IU_PER_PO_ENTRY + MIN_PO_IU), DCM2_PER_IU   );

       for ( pi = 0 ; pi < NUM_PI_ENTRIES ; pi++ )
       {
          if ((pi%8)==0)
              printf ( "\n/*Pi=%5lx*/ ", (long)pi * IU_PER_PI_ENTRY + MIN_PI_IU );

          if ( pi == (NUM_PI_ENTRIES-1) ) printf ( "0x%-4lx   ", jdp_tab[pi][po] );
          else                            printf ( "0x%-4lx,  ", jdp_tab[pi][po] );

       }
          if ( po == (NUM_PO_ENTRIES-1) ) printf ( " } \n");
          else                            printf ( " },\f\n\n\n");
   }

   printf ( "};\n");

   printf ( "#pragma SECTION UNDO\n" );

   return 0;

}

